<?php
declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Service\RechargeService;
use Hyperf\Di\Annotation\Inject;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Crypto\AesGcm;
use WeChatPay\Formatter;

#[AutoController]
class UwxController extends BaseController
{
    #[Inject]
    private RechargeService $rechargeService;

    public function check_signature(RequestInterface $request, ResponseInterface $response){
        # 验证token问题
//        $applet_id = $request->input("applet_id");
        $all = $request->all();
        print_r('----------------消息推送');
        print_r($request->all());
        #从订单中获取小程序token
        $signature = $request->input("signature");
        $timestamp = $request->input("timestamp");
        $nonce =  $request->input("nonce");
        $echostr =  $request->input("echostr");
        $order_id =  $request->input("OutTradeNo");
        $get_applet_id = $this->rechargeService->get_applet_id($order_id);
        if($get_applet_id) {
            $applet_id = "$get_applet_id->applet_id";
        } else {
            var_dump('获取不到小程序ID');
            return $echostr;
        }
        $applet_info = $this->rechargeService->get_applet($applet_id);
        var_dump($applet_info);
        var_dump('虚拟支付回调小程序信息');
        $token = $applet_info['token'];



        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if ($tmpStr == $signature ) {
            if($all['Event'] == 'xpay_coin_pay_notify') {
                $this->rechargeService->buyback($all,1);
            }
            echo "true".PHP_EOL;
            return $echostr;
        } else {
            echo "false".PHP_EOL;
            return $echostr;
        }
    }

    public function wechat_bak(RequestInterface $request, ResponseInterface $response){
        # 验证token问题
//        $applet_id = $request->input("applet_id");
        $apiV3Key = 'R9e223dAp6WQ1zyNt8jgLOFx5hkcMHV7';
        $result = $request->all();
        print_r('-------------------------微信支付');

        $text = base64_decode($result['resource']['ciphertext']); //解密
        /* =========== 使用V3支付需要PHP7.2.6安装sodium扩展才能进行解密参数  ================ */
        $str = sodium_crypto_aead_aes256gcm_decrypt($text, $result['resource']['associated_data'], $result['resource']['nonce'], $apiV3Key);
        $res = json_decode($str, true);
        var_dump($res);
        //如果成功返回了
        if($res['trade_state'] == 'SUCCESS'){
            $this->rechargeService->buyback($res,0);
            return true;
        }
//        $token = 'KDG5BfiCIULEUVWB';
//        $signature = $request->input("signature");
//        $timestamp = $request->input("timestamp");
//        $nonce =  $request->input("nonce");
//        $echostr =  $request->input("echostr");
//
//        $tmpArr = array($token, $timestamp, $nonce);
//        sort($tmpArr, SORT_STRING);
//        $tmpStr = implode( $tmpArr );
//        $tmpStr = sha1( $tmpStr );
//
//        if ($tmpStr == $signature ) {
//            if($all['Event'] == 'xpay_coin_pay_notify') {
//                $this->rechargeService->buyback($all);
//            }
//            echo "true".PHP_EOL;
//            return $echostr;
////            return true;
//        } else {
//            echo "false".PHP_EOL;
//            return $echostr;
////            return false;
//        }
    }
}